package com.jk.lesson3;

import com.jk.lesson2.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * Created by Auser on 2016/11/10.
 */
public class AppleNegate {

    public static void main(String...args){
        Predicate<Apple> redApple = apple -> "red".equals(apple.getColor());
        Predicate<Apple> notRedApple = redApple.negate();
        Predicate<Apple> heavyApple = apple -> apple.getWeight() > 150;
        List<Apple> apples = Arrays.asList(new Apple("red", 120)
                , new Apple("green", 125)
                , new Apple("red", 155)
                , new Apple("green", 155)
                , new Apple("yellow", 120));
        Predicate<Apple> redAndHeavyApple = redApple.and(heavyApple);
        List<Apple> res = filter(apples, redAndHeavyApple);
        System.out.println(res.toString());

        Predicate<Apple> redAndHeavyOrYellowApple = redAndHeavyApple.or(apple -> "yellow".equals(apple.getColor()));
        res = filter(apples, redAndHeavyOrYellowApple);
        System.out.println(res.toString());


        List<Apple> list = filter(apples,apple -> "green".equals(apple.getColor()));
        List<Apple> list2 = filter(apples,apple -> "yellow".equals(apple.getColor()));
        System.out.println("======>"+list.toString());

    }

    public static <T> List<T> filter(List<T> list, Predicate<T> p){
        List<T> res = new ArrayList<>();
        for(T t : list)
            if(p.test(t))
                res.add(t);
        return res;
    }
}
